/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: message.h
  *
  * Purpose: ipc communication message and message queue implementation for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-07-29
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_RUNTIME_MESSAGE_H__
#define __PANDA_COM_RUNTIME_MESSAGE_H__
#include <memory>
#include <functional>
#include "panda/com/com_types.h"
#include "panda/com/runtime/ipc_types.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define MakeMessagePtr() std::make_shared<panda::com::runtime::Message>()
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace com 
{
namespace runtime 
{

enum class MessageType : uint8_t
{
    kEvent = 0x0, /** event message (skeleton -> proxy) */
    kEventSubscribe = 0x01, /** proxy subscirbe a event from skeleton (proxy -> skeleton) */
    kEventSubscribeAck = 0x02, /** return the subscribe state (skeleton -> proxy) */
    kEventUnsubscribe = 0x03, /** unsubscribe a event (proxy -> skeleton) */
    kEventUnsubscribeAck = 0x04, /** return unsubscribe state (skeleton -> proxy */

    kFeildGet = 0x10,     /** get feild value from skeleton (proxy -> skeleton) */
    kFeildGetAck = 0x11,  /** return got value from skeleton feild (skeleton - > proxy) */
    kFeildSet = 0x12,    /** set a value to skeleton feild (proxy -> skeleton) */
    kFeildSetAck = 0x13, /** return the new value after feild set (skeleton - > proxy) */
    kFeildNotify = 0x14, /** notify feild changed value (skeleton -> proxy) */
    kRequest = 0x20,  /** request a method call (proxy -> skeleton) */
    kResponse = 0x21, /** return the result of after method call (skeleton -> proxy) */

    kError = 0x91, /** error from skeleton to proxy (skeleton -> proxy) */
};


struct Message
{  
    MessageType type = MessageType::kEvent; /**todo, refineme?? what default type set as */
    int32_t clientId = -1;
    uint32_t sessionId = 0; /**like packet id, a unique sequence */    
    uint32_t payloadSize = 0;
    PayloadPtr payload = nullptr;
    std::string name = "";
};

using MessagePtr = std::shared_ptr<Message>;
/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/


class MessageQueue
{
public:
    MessageQueue();
    ~MessageQueue();
public:
    void push(MessagePtr msg);
    MessagePtr popAndWait();
    void disableWait();
    void enableWait();
private:
    class impl;
    std::unique_ptr<impl> mImpl;
};

} /** namespace runtime */
} /** namespace com */
} /** namespace panda */


#endif /** !__PANDA_COM_RUNTIME_MESSAGE_H__ */